package com.shiku.imserver.common.utils;

import com.shiku.imserver.common.Protocol;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BufferUtil {
    private static final ThreadLocal<Calendar> localCalendar = new ThreadLocal<>();
    private static Logger log = LoggerFactory.getLogger(BufferUtil.class);

    public static byte read(ByteBuffer buffer) {
        return buffer.get();
    }

    public static int readUB2(ByteBuffer buffer) {
        return (buffer.get() & 255) | ((buffer.get() & 255) << 8);
    }

    public static int readUB3(ByteBuffer buffer) {
        return (buffer.get() & 255) | ((buffer.get() & 255) << 8) | ((buffer.get() & 255) << Protocol.FIRST_BYTE_MASK_4_BYTE_LENGTH);
    }

    public static long readUB4(ByteBuffer buffer) {
        return ((long) (buffer.get() & 255)) | (((long) (buffer.get() & 255)) << 8) | (((long) (buffer.get() & 255)) << 16) | (((long) (buffer.get() & 255)) << 24);
    }

    public static int readInt(ByteBuffer buffer) {
        return (buffer.get() & 255) | ((buffer.get() & 255) << 8) | ((buffer.get() & 255) << Protocol.FIRST_BYTE_MASK_4_BYTE_LENGTH) | ((buffer.get() & 255) << 24);
    }

    public static float readFloat(ByteBuffer buffer) {
        return Float.intBitsToFloat(readInt(buffer));
    }

    public static long readLong(ByteBuffer buffer) {
        return buffer.getLong();
    }

    public static double readDouble(ByteBuffer buffer) {
        return buffer.getDouble();
    }

    public static String readString(ByteBuffer buffer) {
        return readString(buffer, null);
    }

    public static String readString(ByteBuffer buffer, String charset) {
        if (!buffer.hasRemaining()) {
            return null;
        }
        return readString(buffer, buffer.limit() - buffer.position(), charset);
    }

    public static String readStringWithNull(ByteBuffer buffer, String charset) {
        String str = null;
        if (buffer.hasRemaining()) {
            int offset = -1;
            int position = buffer.position();
            int length = buffer.limit();
            boolean needPlus1 = true;
            int i = position;
            while (true) {
                if (i >= length) {
                    break;
                } else if (buffer.get(i) == 0) {
                    offset = i;
                    break;
                } else {
                    i++;
                }
            }
            if (offset == -1) {
                needPlus1 = false;
                offset = buffer.limit();
            }
            if (offset > position) {
                str = readString(buffer, offset - buffer.position(), charset);
                if (needPlus1) {
                    buffer.position(buffer.position() + 1);
                }
            } else {
                buffer.position(buffer.position() + 1);
            }
        }
        return str;
    }

    /* JADX INFO: Multiple debug info for r7v0 int: [D('length' int), D('bytelength' int)] */
    public static String readString(ByteBuffer buffer, int bytelength, String charset) {
        byte[] dst = new byte[bytelength];
        buffer.get(dst, 0, bytelength);
        if (charset == null) {
            return new String(dst);
        }
        try {
            return new String(dst, charset);
        } catch (UnsupportedEncodingException e) {
            log.error(e.toString(), e);
            return new String(dst);
        }
    }

    public static String readStringWithNull(ByteBuffer buffer) {
        return readStringWithNull(buffer, null);
    }

    public static Time readTime(ByteBuffer buffer) {
        move(6, buffer);
        int hour = read(buffer);
        int minute = read(buffer);
        int second = read(buffer);
        Calendar cal = getLocalCalendar();
        cal.set(0, 0, 0, hour, minute, second);
        return new Time(cal.getTimeInMillis());
    }

    public static Date readDate(ByteBuffer buffer) {
        byte length = read(buffer);
        int year = readUB2(buffer);
        byte month = read(buffer);
        byte date = read(buffer);
        int hour = read(buffer);
        int minute = read(buffer);
        int second = read(buffer);
        if (length == 11) {
            long nanos = readUB4(buffer);
            Calendar cal = getLocalCalendar();
            cal.set(year, (byte) (month - 1), date, hour, minute, second);
            Timestamp time = new Timestamp(cal.getTimeInMillis());
            time.setNanos((int) nanos);
            return time;
        }
        Calendar cal2 = getLocalCalendar();
        cal2.set(year, (byte) (month - 1), date, hour, minute, second);
        return new java.sql.Date(cal2.getTimeInMillis());
    }

    public static void move(int i, ByteBuffer buffer) {
        buffer.position(buffer.position() + i);
    }

    public static void position(int i, ByteBuffer buffer) {
        buffer.position(i);
    }

    public static final void writeUB2(ByteBuffer buffer, int i) {
        buffer.put((byte) (i & 255));
        buffer.put((byte) (i >>> 8));
    }

    public static final void writeUB3(ByteBuffer buffer, int i) {
        buffer.put((byte) (i & 255));
        buffer.put((byte) (i >>> 8));
        buffer.put((byte) (i >>> 16));
    }

    public static final void writeInt(ByteBuffer buffer, int i) {
        buffer.put((byte) (i & 255));
        buffer.put((byte) (i >>> 8));
        buffer.put((byte) (i >>> 16));
        buffer.put((byte) (i >>> 24));
    }

    public static final void writeFloat(ByteBuffer buffer, float f) {
        writeInt(buffer, Float.floatToIntBits(f));
    }

    public static final void writeUB4(ByteBuffer buffer, long l) {
        buffer.put((byte) ((int) (255 & l)));
        buffer.put((byte) ((int) (l >>> 8)));
        buffer.put((byte) ((int) (l >>> 16)));
        buffer.put((byte) ((int) (l >>> 24)));
    }

    public static final void writeLong(ByteBuffer buffer, long l) {
        buffer.put((byte) ((int) (255 & l)));
        buffer.put((byte) ((int) (l >>> 8)));
        buffer.put((byte) ((int) (l >>> 16)));
        buffer.put((byte) ((int) (l >>> 24)));
        buffer.put((byte) ((int) (l >>> 32)));
        buffer.put((byte) ((int) (l >>> 40)));
        buffer.put((byte) ((int) (l >>> 48)));
        buffer.put((byte) ((int) (l >>> 56)));
    }

    public static final void writeDouble(ByteBuffer buffer, double d) {
        writeLong(buffer, Double.doubleToLongBits(d));
    }

    public static final void writeLength(ByteBuffer buffer, long l) {
        if (l < 251) {
            buffer.put((byte) ((int) l));
        } else if (l < 65536) {
            buffer.put((byte) -4);
            writeUB2(buffer, (int) l);
        } else if (l < 16777216) {
            buffer.put((byte) -3);
            writeUB3(buffer, (int) l);
        } else {
            buffer.put((byte) -2);
            writeLong(buffer, l);
        }
    }

    public static final void writeWithNull(ByteBuffer buffer, byte[] src) {
        buffer.put(src);
        buffer.put((byte) 0);
    }

    public static final void writeWithLength(ByteBuffer buffer, byte[] src) {
        int length = src.length;
        if (length < 251) {
            buffer.put((byte) length);
        } else if (((long) length) < 65536) {
            buffer.put((byte) -4);
            writeUB2(buffer, length);
        } else if (((long) length) < 16777216) {
            buffer.put((byte) -3);
            writeUB3(buffer, length);
        } else {
            buffer.put((byte) -2);
            writeLong(buffer, (long) length);
        }
        buffer.put(src);
    }

    public static final void writeWithLength(ByteBuffer buffer, byte[] src, byte nullValue) {
        if (src == null) {
            buffer.put(nullValue);
        } else {
            writeWithLength(buffer, src);
        }
    }

    public static final int getLength(long length) {
        if (length < 251) {
            return 1;
        }
        if (length < 65536) {
            return 3;
        }
        if (length < 16777216) {
            return 4;
        }
        return 9;
    }

    public static final int getLength(byte[] src) {
        int length = src.length;
        if (length < 251) {
            return length + 1;
        }
        if (((long) length) < 65536) {
            return length + 3;
        }
        if (((long) length) < 16777216) {
            return length + 4;
        }
        return length + 9;
    }

    private static final Calendar getLocalCalendar() {
        Calendar cal = localCalendar.get();
        if (cal != null) {
            return cal;
        }
        Calendar cal2 = Calendar.getInstance();
        localCalendar.set(cal2);
        return cal2;
    }
}
